package com.bitrate.dagger.sdk.websocket.sender;

import java.util.Arrays;

import com.bitrate.dagger.sdk.websocket.dto.WebSocketPushDTO;
import com.bitrate.dagger.sdk.websocket.request.WebSocketRequest;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

/**
 * WebSocket推送
 */
@Data
@Slf4j
public class WebSocketSender {

	private String host;// 推送平台的请求地址

	private String commonKey;// 推送平台颁发的签名

	public WebSocketSender(String host, String commonKey) {
		this.host = host + "/websocket-api/push";
		this.commonKey = commonKey;
	}

	public String push(WebSocketRequest request, String... channelArr) {
		WebSocketPushDTO pushDto = new WebSocketPushDTO();
		pushDto.setRequest(request);
		pushDto.setCommonKey(commonKey);
		pushDto.setChannelArr(Arrays.asList(channelArr));
		String jsonBody = JSONUtil.toJsonStr(pushDto);
		log.info("WebSocket推送请求参数:{}", jsonBody);
		HttpResponse response = HttpRequest.post(this.host).body(jsonBody).execute();
		return response.body();
	}

}
